/**
 * 
 */
package com.spatialkey.scheduledtasks;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.spatialkey.scheduledtasks.quartz.SpatialKeyQuartzJobBean;

/**
 * Copyright (c) 2008 Universal Mind, Inc.
 * @author Anthony McClure
 * Date: Mar 13, 2009
 * Time: 10:09:20 AM
 *
 */
public class DailyFileCleanupJob extends SpatialKeyQuartzJobBean {
	
	private static Logger logger = Logger.getLogger("com.spatialkey.scheduledtasks.DailyFileCleanupJob");

	/* (non-Javadoc)
	 * @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)
	 */
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		logger.info("[DailyFileCleanupJob] - running file clean up job");
		
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.endsWith(".csv") || name.endsWith(".umg") ||
						name.endsWith(".xml") ||
						name.endsWith(".zip"))
					return true;
				else
					return false;
			}
		};
		
		//get 2 days ago date
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.DATE, -2);
		
		File dir = new File("spatialkey-datapublisher.out");
		File[] files = dir.listFiles(filter);
				
		if (files != null)
		{
			//check files for date and delete if needed
			for (File thisFile : files)
			{
				if (new Date(thisFile.lastModified()).before(cal.getTime()))
				{
					//delete the file
					thisFile.delete();
				}
			}
		}
		
		logger.info("[DailyFileCleanupJob] - finished file clean up job");
	}

}
